package org.cos.generation.epda;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;

public class Method {
	
	protected String qualifiedType;
	
	protected String simpleName;
	
	protected List<String> paraTypes;
	
	protected boolean returnFlag;
	
	public Method(String qualifiedType, String simpleName,
			List<String> paraTypes, boolean returnFlag) {
		super();
		this.qualifiedType = qualifiedType;
		this.simpleName = simpleName;
		this.paraTypes = paraTypes;
		this.returnFlag = returnFlag;
	}
	
	public static Method valueOf(IMethodBinding methodBinding) {
		if (methodBinding != null) {
			ITypeBinding typeBinding = methodBinding.getDeclaringClass();
			if (typeBinding != null) {
				String qualifiedType = typeBinding.getQualifiedName();
				String simpleName = methodBinding.getName();
				ITypeBinding typeBindings[] = methodBinding.getParameterTypes();
				List<String> paraTypes = new ArrayList<String>(typeBindings.length);
				for (ITypeBinding para : typeBindings) {
					paraTypes.add(para.getQualifiedName());
				}
				return new Method(qualifiedType, simpleName, paraTypes, false);
			} else {
				throw new NullPointerException();
			}
		} else {
			throw new NullPointerException();
		}
	}
	
	public static Method valueOfReturn(Method method) {
		return new Method(method.getQualifiedType(), method.getSimpleName(),
				method.getParaTypes(), true);
	}

	public List<String> getParaTypes() {
		return paraTypes;
	}

	public String getSimpleName() {
		return simpleName;
	}
	
	public String getQualifiedType() {
		return qualifiedType;
	}
	
	public String getFullName() {
		return qualifiedType + "." + simpleName;
	}
	
	public String toString() {
		String str = new String(qualifiedType);
		str += ".";
		str += simpleName;
		str += "(";
		for (String para : paraTypes) {
			str += para;
			str += ",";
		}
		if (str.endsWith(",")) {
			str = str.substring(0, str.length() - 1);
		}
		str += ")";
		return str; 
	}
	
	public int hashCode() {
		return this.toString().hashCode();
	}
	
	public boolean equals(Object o) {
		return this.hashCode() == o.hashCode();
	}

	public Boolean isReturn() {
		return returnFlag;
	}
	
}